package com.lagou.jdbc01;

import java.sql.*;

/**
 * @Author wsl
 * @Date 2021/8/18 19:53
 * @Project_Name mysql
 */
public class JdbcDemo02 {

    public static void main(String[] args) throws Exception {

        //1.注册驱动
        Class.forName("com.mysql.jdbc.Driver");

        //2.获取连接
        String url = "jdbc:mysql://localhost:3306/db4?characterEncoding = UTF-8";
        Connection con = DriverManager.getConnection(url, "root", "123456");

        //3.获取statement对象
        Statement statement = con.createStatement();

        //4.执行查询操作，使用executeQuery方法
        String sql = "select * from jdbc_user";
        //返回结果集对象
        ResultSet resultSet = statement.executeQuery(sql);
        //处理结果集对象 resultSet
        /*boolean next = resultSet.next();//判断是否有下一条数据
        System.out.println(next);//true*/

        //获取id
        /*int id = resultSet.getInt("id");
        System.out.println("通过列名的方式获取："+id);*/

        /*int id = resultSet.getInt(1);
        System.out.println("通过列号的方式获取："+id);*/

        //通过while循环，遍历获取resultSet中的数据
        while (resultSet.next()){
            //获取id
            int id = resultSet.getInt("id");
            //获取姓名
            String username = resultSet.getString("username");
            //获取密码
            String password = resultSet.getString("password");
            //获取生日
            Date birthday = resultSet.getDate("birthday");

            System.out.println(id+" : "+username+" : "+password+" : "+birthday);
        }

        //5.关闭流
        resultSet.close();
        statement.close();
        con.close();
    }
}
